package com.miracle.blog.handler;

import com.alibaba.fastjson.JSON;
import com.miracle.blog.vo.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static com.miracle.blog.constant.CommonConst.APPLICATION_JSON;

/**
 * 登录失败处理
 *
 */
@Component
public class AuthenticationFailHandlerImpl implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        String password = httpServletRequest.getParameter("password");
        System.out.println(httpServletRequest.getParameter("username"));
        System.out.println(password);
        httpServletResponse.setContentType(APPLICATION_JSON);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication);
        httpServletResponse.getWriter().write(JSON.toJSONString(Result.fail(e.getMessage())));
    }

}
